import datetime
import csv
import StringIO

def cluster (currn, count, values):
    return ( map(str, map(clean, values[:int(count)])), currn+int(count))

def clean(x):
    x = x.replace("\r\n","\n").strip()
    return "\n".join([line.strip() for line in x.split("\n")])    

def parseqns (questions, type, values, cnt):
    q,cnt = (clean(values[cnt]),cnt+1)
    questions.append(q)
    if type == 'mcqcb' or type == 'mcqrb':
        count,cnt = (values[cnt],cnt+1) #count for the number of options
        questions.append(count)
        q,cnt = cluster(cnt, count, values[cnt:])
        questions.extend(q)
    if type == 'passage':
        pass
    return (questions,cnt)    

contents = """abc,1,name,sri,2008:9:23:11:0:0,2008:9:23:16:0:0,3,1,2,3,mcqrb,mcqcb,mcqcb,q1,2,q11,q12,q2,3,q21,q22,q23,q3,2,q31,q32,"1","1,3","2",10,20,10"""

for x in csv.reader(StringIO.StringIO(contents)):
    if not x :
        continue
    cnt = 0
    email,cnt = (x[cnt],cnt+1)
    count,cnt = (x[cnt],cnt+1) #count for the number of name value pairs
    details,cnt = cluster(cnt, 2*int(count), x[cnt:])
    test_start_time,cnt = (eval("datetime.datetime ("+str(map( int, x[cnt].split(':')))[1:-1]+")"),cnt+1) #Converts date time string to
    test_end_time,cnt = (eval("datetime.datetime ("+str(map( int, x[cnt].split(':')))[1:-1]+")"),cnt+1)   #datetime in python
    state = 0
    count,cnt = (x[cnt],cnt+1) #count for the number of grops, types, questions.
    group,cnt = cluster(cnt, count, x[cnt:])
    qtype,cnt = cluster(cnt, count, x[cnt:])
    questions=[]
    for y in qtype: 
        questions,cnt = parseqns (questions, y, x, cnt)    
    exp_ans,cnt = cluster(cnt, count, x[cnt:])                
    str_max_mark,cnt = cluster(cnt, count, x[cnt:])
    prob_max_mark = map(int, str_max_mark)
    given_ans = ['' for z in range(int(count))] 
    prob_stud_marks = [0 for z in range(int(count))]
    total_stud_mark = 0
    qns_done = 0
    qstrings_done = 0
    
    print email, test_start_time,test_end_time,group, qtype,questions,exp_ans,prob_max_mark

